package com.ruoyi.system.controller.person;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.Emplyee;
import com.ruoyi.system.domain.Evection;
import com.ruoyi.system.dto.ChangeStatusDto;
import com.ruoyi.system.service.IEmplyeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.AskLeave;
import com.ruoyi.system.service.IAskLeaveService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 请假申请Controller
 *
 * @author ruoyi
 * @date 2023-12-22
 */
@RestController
@RequestMapping("/askLeave")
public class AskLeaveController extends BaseController
{
    @Autowired
    private IAskLeaveService askLeaveService;
    @Autowired
    private IEmplyeeService emplyeeService;
    /**
     * 查询请假申请列表
     */
    @RequiresPermissions("system:askLeave:list")
    @GetMapping("/list")
    public TableDataInfo list(AskLeave askLeave)
    {
        startPage();
        List<AskLeave> list = askLeaveService.selectAskLeaveList(askLeave);
        return getDataTable(list);
    }

    /**
     * 导出请假申请列表
     */
    @RequiresPermissions("system:askLeave:export")
    @Log(title = "请假申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AskLeave askLeave)
    {
        List<AskLeave> list = askLeaveService.selectAskLeaveList(askLeave);
        ExcelUtil<AskLeave> util = new ExcelUtil<AskLeave>(AskLeave.class);
        util.exportExcel(response, list, "请假申请数据");
    }

    /**
     * 获取请假申请详细信息
     */
    @RequiresPermissions("system:askLeave:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(askLeaveService.selectAskLeaveById(id));
    }

    /**
     * 新增请假申请
     */
    @RequiresPermissions("system:askLeave:add")
    @Log(title = "请假申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AskLeave askLeave)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        System.out.println("sysUser:"+sysUser);
        Emplyee emplyee = emplyeeService.selectEmplyeeByEno(sysUser.getEno());
        System.out.println("当前登录员工："+emplyee.getName());
        askLeave.setName(emplyee.getName());
        askLeave.setEno(sysUser.getEno());
        askLeave.setStatus(1);
        //计算相差天数
        // 转换为Instant
        Instant startInstant = askLeave.getLeaveDate().toInstant();
        Instant endInstant = askLeave.getReturnDate().toInstant();
        // 计算相差的天数
        long daysDifference = ChronoUnit.DAYS.between(startInstant, endInstant);
        System.out.println("相差"+daysDifference+"天");
        askLeave.setTime(daysDifference+"天");
        return toAjax(askLeaveService.insertAskLeave(askLeave));
    }

    /**
     * 修改请假申请
     */
    @RequiresPermissions("system:askLeave:edit")
    @Log(title = "请假申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AskLeave askLeave)
    {
        return toAjax(askLeaveService.updateAskLeave(askLeave));
    }


    /**
     * 修改审批状态
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ChangeStatusDto dto)
    {
        AskLeave askLeave = new AskLeave();
        askLeave.setId(dto.getId());
        askLeave.setStatus(dto.getStatus().intValue());
        return toAjax(askLeaveService.updateAskLeave(askLeave));
    }

    /**
     * 删除请假申请
     */
    @RequiresPermissions("system:askLeave:remove")
    @Log(title = "请假申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(askLeaveService.deleteAskLeaveByIds(ids));
    }
}
